package com.example.shiroredis.utils;

import java.text.DecimalFormat;
import java.util.*;

public class GetTool {

	public static boolean checkCodeIdIsValidate(String codeId, int length) {
		if (codeId.length() != length) {
			return false;
		}
		return true;
	}

	public static String getFloatTwo(float d) {

		DecimalFormat df = new DecimalFormat("0.00");
		return df.format(d);
	}

	public static String getFloatTwo(double x) {
		DecimalFormat df = new DecimalFormat("0.00");
		return df.format(x);

	}

	public static Double getDoubleTwo(Double d) {
		if (d == null) {
			d = (double) 0;
		}
		DecimalFormat df = new DecimalFormat("0.00");
		// System.out.println(Double.parseDouble(df.format(d)));
		return Double.parseDouble(df.format(d));
	}

	public static double getDoubleTwo(Integer d) {
		if (d == null) {
			d = (Integer) 0;
		}
		DecimalFormat df = new DecimalFormat("0.00");
		// System.out.println(Double.parseDouble(df.format(d)));
		return Double.parseDouble(df.format(d));
	}

	public static String[] splitStr(String Str) {
		String[] types = new String[2];
		if (Str.indexOf("-") == 0) {
			types[0] = "";
			types[1] = Str.split("-")[0];
		} else {

			if (Str.split("-").length == 1) {
				types[0] = Str.split("-")[0];
				types[1] = "";
			} else {
				types = Str.split("-");
			}
		}
		return types;
	}

	/**
	 * 从字符串中删除指定字符串
	 * 
	 * @param needDelete
	 * @param oldStr
	 * @return
	 */
	public static String deleteStr(String needDelete, String oldStr) {

		while (true) {
			int index = oldStr.indexOf(needDelete);
			if (index > -1) {
				oldStr = oldStr.substring(0, index)
						+ oldStr.substring(index + needDelete.length(),
								oldStr.length());
			} else {
				break;
			}
		}
		return oldStr;
	}

	/**
	 * list 中 获取最小值
	 * 
	 * @param ins
	 * @return
	 */
	public static int getMin(List<Integer> ins) {
		Object[] objs = ins.toArray();
		Arrays.sort(objs);
		return Integer.valueOf(String.valueOf(objs[0]));
	}

	/**
	 * 将map根据固定格式转化成字符串
	 * 
	 * @author ZHL
	 * 
	 * @param needDelete
	 * @param oldStr
	 * @return String
	 * 
	 * @date 2017-3-23
	 */
	public static String MapconversionStr(Map<String, String> params) {
		List<String> keys = new ArrayList<String>(params.keySet());
		Collections.sort(keys);
		String prestr = "";
		for (int i = 0; i < keys.size(); i++) {
			String key = keys.get(i);
			String value = params.get(key);

			if (i == keys.size() - 1) {// 拼接时，不包括最后一个&字符
				prestr = prestr + key + "=" + value;
			} else {
				prestr = prestr + key + "=" + value + "&";
			}
		}
		return prestr;
	}

	private static String[] strs = new String[] { "a", "b", "c", "d", "e", "f",
			"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
			"t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F",
			"G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
			"T", "U", "V", "W", "X", "Y", "Z" };

	public static String CreatenNonce_str() {
		Random r = new Random();
		StringBuilder sb = new StringBuilder();
		int length = strs.length;
		for (int i = 0; i < 15; i++) {
			sb.append(strs[r.nextInt(length - 1)]);
		}
		return sb.toString();
	}

}
